
#include <stdint.h>
#include <Wire.h>
#include "Arduino.h"   // For interrupts/noInterrupts

#include "debug.h"
#include "i2c.h"

// ===================================================================
//   I2C_setup
// ===================================================================
void I2C_setup (void)
{
  //
  // This code detects the address of the I2C devices; we're looking for the
  // LCD.
  //
  // This sketch tests the standard 7-bit addresses
  // Devices with higher bit address might not be seen properly.
  //
  Wire.begin();
  
  PRINTF ("Scanning for the I2C devices ...\n");

  uint8_t error, address;
  uint32_t nDevices = 0;

  for (address = 1; address < 127; address++ ) {
    //
    // The i2c_scanner uses the return value of the Write.endTransmisstion 
    // to see if a device did acknowledge to the address.
    //
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
      
    if (error == 0) {
      //Serial.print("I2C device found at address 0x");
      //if (address<16)
      //	Serial.print("0");
      //Serial.print(address,HEX);
      //Serial.println("  !");

      PRINTF ("I2C device found at address 0x%02x \n", address);
	  
      nDevices++;
    }
    else if (error==4) {
      //Serial.print("Unknow error at address 0x");
      //if (address<16)
      //	Serial.print("0");
      //Serial.println(address,HEX);
      PRINTF ("Unknow error at address 0x%02x \n", address);
    }    
  }

  if (nDevices == 0) {
    //Serial.println("No I2C devices found\n");
    PRINTF ("No I2C devices found\n");
  }
  else {
    //Serial.println("done\n");
    PRINTF ("Found %d I2C devices \n", nDevices);
  }

  PRINTF ("Scanning for the I2C devices complete.\n");

  return;
}
